from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import BolgDriver

class BlogLogin:
    url=""
    driver=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver=BolgDriver.driver
        self.driver.get(self.url)
    #正常登录
    def LoginSucTest(self):
        #先清空输入框
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        #输入正确的账号和密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #查找头像，用户名和github地址
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > img")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > a")
        #截图
        BolgDriver.getScreenShot()
        #回到登录界面
        self.driver.back()

    #异常登录
    def LoginFailTest(self):
        # 先清空输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入错误的账号
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhang")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #出现警告弹窗：用户不存在
        WebDriverWait(self.driver,3).until(expected_conditions.alert_is_present())
        alert=self.driver.switch_to.alert
        assert alert.text=="用户不存在"
        alert.accept()
        #截图
        BolgDriver.getScreenShot()

        # 输入正确的账号和错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 出现警告弹窗：密码错误
        WebDriverWait(self.driver, 3).until(expected_conditions.alert_is_present())
        alert = self.driver.switch_to.alert
        assert alert.text == "密码错误"
        alert.accept()
        #截图
        BolgDriver.getScreenShot()

        # 输入空的账号
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 出现警告弹窗：账号或密码不能为空
        WebDriverWait(self.driver, 3).until(expected_conditions.alert_is_present())
        alert = self.driver.switch_to.alert
        assert alert.text == "账号或密码不能为空"
        alert.accept()
        #截图
        BolgDriver.getScreenShot()

        # 输入空的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhang")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 出现警告弹窗：账号或密码不能为空
        WebDriverWait(self.driver, 3).until(expected_conditions.alert_is_present())
        alert = self.driver.switch_to.alert
        assert alert.text == "账号或密码不能为空"
        alert.accept()
        #截图
        BolgDriver.getScreenShot()

# login=BlogLogin()
# login.LoginSucTest()
# login.LoginFailTest()